package com.zscat.mallplus.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.oms.entity.OmsOrderItem;
import com.zscat.mallplus.oms.service.IOmsOrderItemService;
import com.zscat.mallplus.pms.entity.PmsComment;
import com.zscat.mallplus.pms.mapper.PmsCommentMapper;
import com.zscat.mallplus.pms.service.IPmsCommentService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.util.IpAddressUtil;
import com.zscat.mallplus.utils.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品评价表 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-19
 */
@Slf4j
@Service
public class PmsCommentServiceImpl extends ServiceImpl<PmsCommentMapper, PmsComment> implements IPmsCommentService {

    @Resource
    private PmsCommentMapper commentMapper;

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IOmsOrderItemService orderItemService;

    @Transactional(rollbackFor = Exception.class)
    public CommonResult saveComment(HttpServletRequest request, PmsComment comment) {
        try {
            UmsMember currentMember = memberService.getNewCurrentMember();
            if (null != currentMember) {
                comment.setMemberId(currentMember.getId());
                comment.setMemberIcon(currentMember.getIcon());
                comment.setMemberNickName(currentMember.getNickname());
                comment.setMemberIp(IpAddressUtil.getIpAddr(request));
            } else {
                return new CommonResult().failed("请先登录");
            }

            // 查询该商品当前会员是否完成评价
            PmsComment pmsComment = commentMapper.selectOne(new QueryWrapper<PmsComment>().eq("order_id", comment.getOrderId()).eq("product_id", comment.getProductId()).eq("member_id", currentMember.getId()));
            if (null == pmsComment) {
                comment.setCreateTime(new Date());
                comment.setShowStatus(0);
                commentMapper.insert(comment);
                OmsOrderItem orderItem = new OmsOrderItem();
//                orderItem.setStatus(OrderStatus.TO_COMMENT.getValue());
                orderItem.setIsComment(1);
                orderItem.setCommentTime(new Date());
                orderItemService.update(orderItem, new QueryWrapper<OmsOrderItem>().eq("order_id", comment.getOrderId()).eq("product_id", comment.getProductId()));
                return new CommonResult().success("商品评价成功");
            } else {
                return new CommonResult().failed("商品已评价");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    /**
     * 查询商品评价列表
     *
     * @param comment
     * @return
     */
    public List<PmsComment> selectCommentList(PmsComment comment) {
        return commentMapper.selectCommentList(comment);
    }
}
